<?php

namespace Application\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Application\Model\RegionsTable;
use Application\Model\PaysTable;
use Application\Model\SejourTable;

class PaysRegionsTable extends PaysTable
{
    protected $tableGateway;
    protected $regionsTable;

    public function __construct(TableGateway $tableGateway, SejourTable $sejourTable, RegionsTable $regionsTable)
    {
        parent::__construct($tableGateway, $sejourTable);

    	$this->tableGateway = $tableGateway;
        $this->regionsTable = $regionsTable;
    }

    public function fetchAllUsed()
    {
        $resultatPays = parent::fetchAllUsed();

        $entities = array();

        foreach ($resultatPays as $key => $pays) {
            
            $obj = new PaysRegions();
            $obj->exchangeArray( (array) $pays);
            
            if( $pays->nombreRegionsAssocie > 0 ) {

                $liste = $this->regionsTable->listeRegionsParIdPays( $pays->idPays );

                $obj->listeRegions = $liste;
            }

            $entities[] = $obj;
        }

        return $entities;
    }

}